#include "Utility.h"

//Checks if the incoming string comprises of only digits. If it is, function
//converts the string to integers and splits them into half. Throws an exception
//if the string does not comprise entirely of numeric values.
void Utility::extractDigits(QString checkParam, int &getFirst, int &getSecond) {
	bool *isNumber;
	
	int value = checkParam.toInt(isNumber, 10);

	if (*isNumber) {
		getFirst = value / 100;
		getSecond = value % 100;
	}
	else {
		throw InvalidInputException("invalid numeric parameters, please task parameters enter in the proper format");
	}
}

//Checks if the incoming string comprises of only digits. If it is, function
//converts the string to integers. Throws an exception if the string does not 
//comprise entirely of numeric values.
void Utility::extractDigits(QString checkParam, int &getDigit) {
	bool *isNumber;
	
	getDigit = checkParam.toInt(isNumber, 10);

	if (*isNumber) {
		return;
	}
	else {
		throw InvalidInputException("invalid numeric parameters, please task parameters enter in the proper format");
	}
}

//Checks if the incoming string comprises of only digits. If it is, function
//returns true, else the function returns false.
bool Utility::isDigit(QString checkParam) {
	bool *isNumber;
	
	int value = checkParam.toInt(isNumber, 10);

	return *isNumber;
}

//Splits parameters into its constituent strings and returns a vector of 
//strings.

//Notes:
//1. Needs to be guarded externally against entering empty strings.
vector<string> Utility::splitParameters(string parameters){
	vector<string> sParameters;

	istringstream iss(parameters);
	copy(istream_iterator<string>(iss), istream_iterator<string>(), 
		 back_inserter<vector<string> >(sParameters));

	return sParameters;
}

//Case insensitive comparator function that returns true if both strings are the same
//ignoring case.
bool Utility::equalsIgnoreCase(const QString& str1, const QString& str2) {
	if (QString::compare(str1, str2, Qt::CaseInsensitive) != 0)
		return false;

    return true;
}

QString Utility::removeWhiteSpace(QString param) {
	param.simplified();

	return param;
}